package services

import (
	"errors"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"time"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/utils"
)

type CuringService struct {
}

func CreateCuringNumber() string {
	stat := utils.GetDateParse(time.Now().Format("2006-01-02"))
	end := stat + 86400
	return fmt.Sprintf("%s%06d", time.Now().Format("20060102150405"), models.QueryCuringSoltSeterCount(stat, end, )+1)
}

//新增计划
func (s *CuringService) CuringCreate(data *models.ErpCuring) (int64, error) {

	o := orm.NewOrm()
	//判断是否有养护计划进行中
	count, _ := o.QueryTable(models.ErpCuringTableName()).
		Filter("DepartmentId", data.DepartmentId).
		Filter("CuringType", data.CuringType).
		Filter("Status", 1).Count()
	if count > 0 {
		return 0, errors.New("存在未完成养护计划请先完成")
	}
	//获取需要养护的产品数量
	params := &models.ErpInventoryQueryParam{}
	params.BrandId = data.BrandId
	params.Department = data.DepartmentId
	params.Typeid = data.ProductTypeid
	params.Isnegative = "1"
	inventorys := models.ErpInventoryList(params)
	if len(inventorys) <= 0 {
		return 0, errors.New("没有需要养护的库存产品")
	}
	data.Number = CreateCuringNumber()
	data.Status = 1
	data.CreatedAt = time.Now().Unix()
	data.NeedCheckNumber = int64(len(inventorys))
	_, err := o.Insert(data)
	if err != nil {
		return 0, err
	}

	insertCuringData := make([]*models.ErpCuringData, 0)
	for k, _ := range inventorys {
		curing_data := new(models.ErpCuringData)
		curing_data.DepartmentId = data.DepartmentId
		curing_data.Curing = data
		curing_data.Products = inventorys[k].Products
		curing_data.Inventory = inventorys[k]
		curing_data.LotNumber = inventorys[k].Acceptance.LotNumber
		curing_data.ManufactureDate = inventorys[k].Acceptance.ManufactureDate
		curing_data.ExpiryDate = inventorys[k].Acceptance.ExpiryDate
		insertCuringData = append(insertCuringData, curing_data)
	}

	if _, err := o.InsertMulti(len(insertCuringData), insertCuringData); err != nil {
		return 0, err
	}

	return data.Id, nil
}

//将需养护商品状态更新为养护中
func (s *CuringService) SetProductCuringDataStatus(curing_id int64, curing_data_id []int64) error {
	if curing_id == 0 {
		return errors.New("参数错误")
	}
	if len(curing_data_id) <= 0 {
		return errors.New("参数错误")
	}

	//清除养护计划中的商品
	to,_ := orm.NewOrm().Begin()
	if _, err := to.Raw("UPDATE "+models.ErpCuringDataTableName()+" SET status=0 WHERE status=1 AND curing_id=?", curing_id).Exec(); err != nil {
		return err
	}
	//更新选择需要养护的商品
	if _, err := to.QueryTable(models.ErpCuringDataTableName()).Filter("Status", 0).Filter("id__in", curing_data_id).Update(orm.Params{"Status": 1}); err != nil {
		to.Rollback()
		return err
	}

	to.Commit()
	return nil
}

//保存商品养护状态
func (s *CuringService) CuringProductCheckSave(curing_id int64, exterior, packing, opinion int, curing_data_id []int64) error {
	if curing_id == 0 {
		return errors.New("参数错误1")
	}
	if exterior == 0 || packing == 0 || opinion == 0 {
		return errors.New("参数错误2")
	}
	if len(curing_data_id) <= 0 {
		return errors.New("参数错误3")
	}
	to,_ := orm.NewOrm().Begin()
	for k, _ := range curing_data_id {
		data_id := curing_data_id[k]
		curing_data := new(models.ErpCuringData)
		curing_data.Exterior = exterior
		curing_data.Packing = packing
		curing_data.Opinion = opinion
		curing_data.Id = data_id
		curing_data.Status = 2
		if _, err := to.Update(curing_data, "Exterior", "Packing", "Opinion", "Status"); err != nil {
			to.Rollback()
			return err
		}
	}
	curing_yes_number := int64(len(curing_data_id))

	//更新计划表已养护数据
	curing, err := models.GetErpCuringById(curing_id)
	if err != nil {
		to.Rollback()
		return err
	}
	curing.AlreadyCheckNumber += curing_yes_number
	if curing.AlreadyCheckNumber >= curing.NeedCheckNumber {
		curing.Status = 2
	}
	if _, err := to.Update(curing, "Status", "AlreadyCheckNumber"); err != nil {
		to.Rollback()
		return err
	}

	to.Commit()
	return nil
}
